<!--
 * @Author: 郑锐 307057322@qq.com
 * @Date: 2023-01-18 10:13:27
 * @LastEditors: 郑锐 307057322@qq.com
 * @LastEditTime: 2023-03-17 17:27:42
 * @FilePath: \SX-ThreeLinesAndSingle\src\views\Login\sLogin.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div v-loading="loading" class="py-5" element-loading-text="加载中，请稍后！" element-loading-spinner="el-icon-loading" />
</template>

<script>
import { Notify } from 'vant'
import { getUserInfo } from '@/api/user'
import { getRefData } from '@/api/common'
import { setToken } from '@/utils/auth'
export default {
  data() {
    return {
      targetList: [
        // { label: '全部', name: '全部' },
        { label: '安全生产', name: '安全', code: 'safety' },
        { label: '环保管理', name: '环保', code: 'environmental' },
        { label: '消防管理', name: '消防', code: 'fireFighting' },
        { label: '能源管理', name: '能源', code: 'energySources' },
        { label: '碳排放管理', name: '碳排放', code: 'carbonDischarge' }
      ],
      loading: true
    }
  },
  mounted() {
    const token = decodeURIComponent(this.$route.query.token)
    if (token) {
      setToken(token)
      this.handleLogin(token)
    }
  },
  methods: {
    async handleLogin(token) {
      const { status, data } = await getUserInfo({ userId: '' })
      if (status === 0 && data) {
        sessionStorage.setItem('rights', JSON.stringify(data.rights))
        sessionStorage.setItem('userInfo', JSON.stringify(data))
        sessionStorage.setItem('roleId', Number(data.userTypeId))
        sessionStorage.setItem('source', 'OAuthSingleLogin')
        const model = this.targetList.filter((x) => {
          return JSON.stringify(data.rights).includes(x.code)
        })
        if (model.length === 0) {
          Notify({ type: 'warning', message: '您暂无权限访问此模块，请联系安全保密员！' })
        } else {
          this.$store.dispatch('app/setCompanyList')
          this.getSelectData()
        }
      }
    },
    async getSelectData() {
      const { status, data } = await getRefData()
      if (status === 0 && data) {
        const selectOptionAll = {}
        data.forEach((v) => {
          selectOptionAll[v.codeTypeName] = v.refDescVOList
            .filter((y) => {
              return y.codeName !== '全部'
            })
            .map((z) => {
              return {
                value: z.codeId,
                label: z.codeName,
                text: z.codeName
              }
            })
        })
        selectOptionAll['行业类别-fixed'] = selectOptionAll['集团业务系统-行业类别']
        localStorage.setItem('selectOptionAll', JSON.stringify(selectOptionAll))
        const jumpUrl = decodeURIComponent(this.$route.query.jumpUrl)
        if (jumpUrl && jumpUrl !== 'undefined') {
          const path = jumpUrl.split('?')[0]
          const taskId = jumpUrl.split('?')[1]
          const type = decodeURIComponent(this.$route.query.type)
          const page = decodeURIComponent(this.$route.query.page)
          if (taskId) {
            const query = new URLSearchParams(taskId)
            const obj = Object.create(null)
            for (const [key, value] of query) {
              obj[key] = value
            }
            const arrItme = {
              safety: '安全生产',
              fireFighting: '消防管理',
              environment: '环保管理'
            }
            this.$router.push({ path: path, query: { ...obj, page, active: '专家排查隐患审核', current: arrItme[type] }})
          }
        } else {
          this.$router.push({ path: '/' })
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
